//
//  ViewController.m
//  jSCoreTest
//
//  Created by rain on 16/8/11.
//  Copyright © 2016年 company. All rights reserved.
//

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "RainWebViewController.h"
/*
概念理解:
 1、JSContext;一个 Context 就是一个 JavaScript 代码执行的环境，也叫作用域
 2、JSValue;JS是弱类型的，ObjectiveC是强类型的，JSValue被引入处理这种类型差异，在 Objective-C 对象和 JavaScript 对象之间起转换作用
 
 Objective-C 和 JavaScript 中类型的对照表：
 Objective-C type  |   JavaScript type
 --------------------+---------------------
 nil            |     undefined
 NSNull         |        null
 NSString       |       string
 NSNumber       |   number, boolean
 NSDictionary   |   Object object
 NSArray        |    Array object
 NSDate         |     Date object
 NSBlock (1)    |   Function object (1)
 id (2)         |   Wrapper object (2)
 Class (3)      | Constructor object (3)
 
 
 
 Blocks ：JS functions （对应 JS 函数）
 JSExport protocol ：JS objects （对应 JS 对象）
 
 注意：
 a、不要在 Block 中直接使用外面的 JSValue 对象, 把 JSValue 当做参数来传进 Block 中。
 b、避免循引用，不要在 Block 中直接引用使用外面的 JSContext 对象，应该用[JSContext currentContext];
 
 JSContext *context = [[JSContext alloc]init];
 context[@"callBack"] = ^(){
    JSValue *vlaue = [JSValue valueWithNewObjectInContext:context];//不可取
    JSValue *vlaue = [JSValue valueWithNewObjectInContext:[JSContext currentContext]];//不可取
    vlaue[@"haha"] = @"right";
 return vlaue
 };
 
*/


@interface ViewController ()

@end

#import <objc/runtime.h>

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 
    JSContext *context = [[JSContext alloc]init];
    JSValue *result = [context evaluateScript:@"2 + 2"];
    NSLog(@"2 + 2 = %d", [result toInt32]);
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(30, 40, 80, 35);
    [button setTitle:@"present" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(presentBtn) forControlEvents:UIControlEventTouchUpInside];
    

}

- (void)presentBtn {
    
    
    RainWebViewController *webVc = [[RainWebViewController alloc]init];
    [self presentViewController:webVc animated:YES completion:^{
        NSString *htmPath = [[NSBundle mainBundle]pathForResource:@"testWebView" ofType:@"htm"];
//        NSURL *htmUrl = [NSURL URLWithString:htmPath];
        webVc.urlStr = htmPath;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
